<?php
namespace BoxEdu\Controller\Admin;
//use Think\Controller;
class ExaminationController extends CommonController
{
    public function _initialize()
    {
        $this->subject = BoxModel("EduSubject");
        $this->subject_class_type = dict("subject_class_type");
        $this->subject_class = $this->subject->subject_class();
        parent::_initialize();
								$this->statusArray=array(
            1 =>'待支付',
            2 =>'已支付',
            3=>'未通过审核',
            4=>'已退款',
            5=>'报名成功',
        );
    }
				//报考查询
		public		function index(){
		   	$this->subject_list = BoxModel("EduSubject")->get(array('status'=>1),"","order_by");
						$this->subject_id=I('subject_id');
						$this->show_fields = BoxModel("addon_data")->get(array('group_name' => "table_apply_examination"), "", "order_by");
						
						$where="1";
						if($this->kw=I('kw')){
						 $where.=" and (data like '%$this->kw%')";
						}
						if($this->status=I('status')){
							 $where.=" and (status = '$this->status')";
						}
						
						if($this->subject_id){
							 $where.=" and (subjectid = '$this->subject_id')";
						}
						
						if($this->start_time=I('start_time')){
						 $where.=" and (add_time > '$this->start_time')";	
						}
						
						if($this->end_time=I('end_time')){
						 $where.=" and (add_time < '$this->end_time')";	
						}
						if(I('search_type')==2){
							 $this->downSearch($where);
						}
						$data=BoxModel("edu_apply_examination")->get($where,"50","id desc");
						$this->list=$data['list'];
						$this->show_page($data['pager']);
					 $this->display();
			}
 function downSearch($where){
				
		  if(!$this->subject_id){
				  $this->jump("请选择下载科目");	
				}
				$this->subjectInfo=BoxModel("edu_subject")->where(array('subjectid'=>$this->subject_id))->find();
				$items=I('items');
			 $ids=implode(",",$items);
				if($ids){
				  $where="id in($ids)";
				}
				$data=BoxModel("edu_apply_examination")->get($where,"","id desc");
				$formGroupName="table_apply_examination_".$this->subject_id;
				$fields=hook("Forms/Index/formGroupFields",$formGroupName);
    $arr=array('subject_name'=>'科目','add_time'=>'报名时间');
				$arr2=array('status'=>'状态');
				$fields=array_merge($arr,$fields,$arr2);
				$items=array();
				$downList=array();
				foreach($data as $dk=>$one){
								$item=array();
								$downKey=$one['name']."_".$one['Idcard'];
								foreach($fields as $k=>$one1){
											 $value=$one[$k];
												if($k=="status"){//状态
												  $value=$this->statusArray[$value];
												}
												if(is_array($value)){
													 if($value['file_url']){//如果是上传的附件
											     $value=ImgDomain."/".$value['file_url'];
																$downList[$downKey][$one1]=$value;
											   }
											  	$item[]=$value;
												}else{
													$item[]=" ".$value;
												}
								}
							$items[]=$item;	
				}
				
				$titles=array();
				foreach($fields as $one)$titles[]=$one;
					if (!class_exists("ZipArchive")){
         $this->jump("no ZipArchive");
		 }
			$path= BOX_PATH . "/Runtime/Down/".date("Ymd")."_".great_rand(10);
			mk_dir($path);
			$obj = new \Common\Org\icPHPExcel(); //初始化本类后，本类将默认操作一个空白的Excel表
			$zipName=$this->subjectInfo['subject']."报考信息";
			$zipName=iconv('UTF-8', 'GB2312',$zipName);
			$filepath = $path . "/".$zipName.".xls";
			$obj->create_from_mysql($titles, $items);
			$obj->saveExcel($filepath);			
			$this->downFiles($downList,$path);	
	}
	
	//下载附件
	public function downFiles($downList,$path){
    foreach($downList as $k=>$files){ //一个人一个文件夹
						$k=iconv('UTF-8', 'GB2312', $k);
						$savePath=$path."/".$k;
					 mk_dir($savePath);
						foreach($files as $k2=>$file){
							  $p=explode(".",$file);
									$k2=iconv('UTF-8', 'GB2312', $k2);
									$name=$k2.".".$p[count($p)-1];
							  $content=mycurl($file);
									saveFile($savePath."/".$name,$content);
						}	
				}
				$zipName=$this->subjectInfo['subject']."报考信息_".date("YmdHis");
				$zipName=iconv('UTF-8', 'GB2312',$zipName);
				$filename=$path."/".$zipName.'.zip';
				\Common\Org\HZip::zipDir($path,$filename); 
				header("Cache-Control: public"); 
				header("Content-Description: File Transfer"); 
				header('Content-disposition: attachment; filename='.$zipName.".zip"); //文件名   
				header("Content-Type: application/zip"); //zip格式的   
				header("Content-Transfer-Encoding: binary"); //告诉浏览器，这是二进制文件    
				header('Content-Length: '. filesize($filename)); //告诉浏览器，文件大小   
				@readfile($filename);
				exit();
				//deldir($path);	
	}
	
	
 
	
//报考设置
    public function subject_class()
    {
        $where = "is_show=1 and parent_id=0";
        if ($this->kw = I('kw')) {
            $where .= " and (subject like '%$this->kw%'  )";
        }
        $this->subject_class_id = I('subject_class_id', "0", "intval");
        if ($this->subject_class_id) {
            $where .= " and (subject_class = '$this->subject_class_id')";
        }
        //	dump($this->subject_class );
        $data = $this->subject->get($where, 10, "subject_class,order_by");
        $this->data = $data['list'];
        $this->page = $data['pager']->show;
        $this->display();
    }

    public function setting()
    {
        $class_id = I('class_id');
        $fee = is_numeric(I('value')) ? I('value') : 0;
        $r = BoxModel("edu_subject")->where(array('subjectid' => $class_id))->save(array('examination_fee' => $fee));
        echo $r;
    }

    public function status()
    {
        $id=I('id');
								$this->data=BoxModel("edu_apply_examination")->load($id);
								if(IS_POST){
								  $status=I('status');
											$r=BoxModel("edu_apply_examination")->where(array('id'=>$id))->save(array('status'=>$status,'remark'=>I('remark')));
											//审核不通过
											if($r && $status==3){
												   $params=array(
														  'title'=>	$this->data['subject_name'],
																'user_id'=>$this->data['user_id'],
																'remark'=>I('remark'),
																'url'=>HomeUrl."/Index/registerForExamination/class_id/".$this->data['classid']."/subjectid/".$this->data['subjectid'],
														);
														Logic("BoxSns","Noti")->send("00010",$params);
											}
											$this->jump("操作成功");
								}
								$this->display();
    }


}